//
//  UserAccount.swift
//  WKweiBo
//
//  Created by 阿拉斯加的狗 on 15/12/9.
//  Copyright © 2015年 阿拉斯加的🐶. All rights reserved.
//

import UIKit

class UserAccount: NSObject,NSCoding {

    //定义模型属性
    
    var access_token: String?
    
    var expires_in: NSTimeInterval = 0 {
    
        didSet{
        
            //转换日期时间
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        
        }
    
    
    }
    
    //计算日期
    var expiresDate: NSDate?
    
    var uid : String?
    
    //用户昵称
    var screen_name: String?
    
    //用户头像
    var avatar_large: String?
    
    //定义kvc构造函数
    init(dict: [String: AnyObject]){
    
        super.init()
    
        setValuesForKeysWithDictionary(dict)
    
    }
    
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    //从写类的description
    override var description: String{
    
        let key = ["access_token", "expires_in", "expiresDate", "uid", "screen_name", "avatar_large"]
        
        return dictionaryWithValuesForKeys(key).description
    
    
    }
    
    //保存当前对象
    func saveUserAccount (){
                                                        //枚举类型可以直接点出来
        var path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last!
        
        //拼接路径
        path = (path as NSString).stringByAppendingString("account.plist")
         
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
    
        //打印路径
        print(path)
    }
    
    
    //归档保存
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        
    }
    
    //解档
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
    }
    
    
    
}
